package cj.web.marriage.controllers;

import java.io.File;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import cj.utils.Constants;
import cj.utils.ImageUtils;
import cj.utils.ReadConfigProperties;
import cj.utils.ReturnUUID;
import cj.web.admin.constants.MemuEnum;
import cj.web.login.controllers.BaseController;
import cj.web.marriage.constants.MarriageVideoTypeEnum;
import cj.web.marriage.dao.MarriageInforDAO;
import cj.web.marriage.dao.MarriageVideoDAO;
import cj.web.marriage.domain.MarriageInfor;
import cj.web.marriage.domain.MarriageVideo;

@Path("marriage")
public class MarriageInforController extends BaseController{
	
	private static Logger logger = Logger.getLogger(MarriageInforController.class.getName());

	@Autowired
	private MarriageInforDAO marriageInforDAO;
	
	@Autowired
	private MarriageVideoDAO marriageVideoDAO;
	
	public static final int marriageInforId = 1;
	
	@Get("show")
	public String show(Model model,MarriageInfor marriageInfor)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		return "/views/marriage/marriageInforList.vm";
	}
	
	@Post("getList")
	public String queryList(MarriageInfor marriageInfor) {
		JSONObject jo = new JSONObject();
		
		List<MarriageInfor> objs = marriageInforDAO.queryList(marriageInfor);
		
		int size = marriageInforDAO.querysize(marriageInfor);
		jo.put("data", objs);
		jo.put("draw", marriageInfor.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model)
	{
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		return "/views/marriage/marriageInforDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,MarriageInfor marriageInfor,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			marriageInforDAO.create(marriageInfor);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") String id)
	{
		MarriageInfor obj = marriageInforDAO.queryById(id);
		if(obj!=null){
			model.add("obj",obj);
		}
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		return "/views/marriage/marriageInforEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,MarriageInfor marriageInfor,HttpServletRequest request)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			marriageInforDAO.update(marriageInfor);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}

	// 置顶
	@Get("stick")
	public String stick(@Param("id") String id) {

		MarriageInfor obj = marriageInforDAO.queryById(id);

		if (obj != null) {
			marriageInforDAO.updateByStickTime(id);
		}

		return "r:show";
	}

	// 取消置顶
	@Get("channelStick")
	public String channelStick(@Param("id") String id) {

		MarriageInfor obj = marriageInforDAO.queryById(id);

		if (obj != null) {
			marriageInforDAO.updateByStickTimeIsNull(id);
		}

		return "r:show";
	}
	
	@Get("video")
	public String video(Model model, @Param("id") String id){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		MarriageInfor obj = marriageInforDAO.queryById(id);
		
		model.add("TypeEnum",EnumSet.allOf(MarriageVideoTypeEnum.class));

		if (obj != null) {
			model.add("obj", obj);			
		}

		return "/views/marriage/videoList.vm";
	}
	
	@Get("showMarriageVideo")
	public String showMarriageVideo(Model model){
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		model.add("TypeEnum",EnumSet.allOf(MarriageVideoTypeEnum.class));

		return "/views/marriage/marriageVideoList.vm";
	}
	
	@Post("getVideoList")
	public String getVideoList(Model model, MarriageVideo marriageVideo){
		JSONObject jo = new JSONObject();
		marriageVideo.setMarriageInforId(marriageInforId);
		List<MarriageVideo> objs = marriageVideoDAO.queryByVideo(marriageVideo);
		
		int size = marriageVideoDAO.querySizeByVideo(marriageVideo);
		jo.put("data", objs);
		jo.put("draw", marriageVideo.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		
		jo.put("data",objs);
		
		return "@" + jo.toString();
	}
	
	@Get("createVideoPage")
	public String createVideoPage(Model model){
		model.add("TypeEnum",EnumSet.allOf(MarriageVideoTypeEnum.class));
		model.add("inforId",marriageInforId);
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		return "/views/marriage/videoDetail.vm";
	}
	
	@Post("createVideo")
	public String createVideo(Model model,MarriageVideo marriageVideo)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {			
			marriageVideoDAO.create(marriageVideo);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("editVideo")
	public String editVideo(Model model,@Param("id") String id)
	{
		MarriageVideo obj = marriageVideoDAO.queryById(id);
		if(obj!=null){
			model.add("obj",obj);
		}
		model.add("TypeEnum",EnumSet.allOf(MarriageVideoTypeEnum.class));
		//MemuEnum
        model.add("MemuEnum",MemuEnum.marriagexinfo);
		return "/views/marriage/videoEdit.vm";
	}
	
	@Post("updateVideo")
	public String updateVideo(Model model,MarriageVideo marriageVideo)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			marriageVideoDAO.update(marriageVideo);
		} catch (Exception e) {
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("delVideo")
	public String delVideo(@Param("id") String id){
		MarriageVideo obj = marriageVideoDAO.queryById(id);
		if(obj!=null){
			marriageVideoDAO.deleteById(id);
			return "r:showMarriageVideo";
		}		
		
		return "r:showMarriageVideo";
		
	}
	
	@Post("doUpload")
    public String doUpload(@Param("uploadimage") MultipartFile file,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		// 判断文件是否为空  
        if (file != null && !file.isEmpty()) {  
            try {  
                // 文件保存路径  
            	String path = ReadConfigProperties.getValue("uploadFilePath_Marriage");
            	//判断文件是否存在，不存在创建文件
            	Map rep = ReturnUUID.getFolder(path);
            	path = rep.get("allpath").toString();//所在的全路径
            	//文件名
                String fileName =  ReturnUUID.getName(file.getOriginalFilename()); 
                // 转存文件  
                file.transferTo(new File(path+"/"+fileName));  
                
                jo.put("filePath", rep.get("filepath")+"/"+fileName);
                
                ImageUtils.scale2(path+"/"+fileName, path+"/s_"+fileName, 120, 120, true);//缩率图
                
            } catch (Exception e) {  
                e.printStackTrace();  
                jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
            }  
        } 
		
		return "@"+jo.toString();
    }
	
	
	
}
